/* $Id$ */
package org.crazydays.gameplan.db.io;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import org.junit.*;
import static org.junit.Assert.*;

import org.crazydays.gameplan.db.Database;
import org.crazydays.gameplan.db.io.DatabaseOutputStream;
import org.crazydays.gameplan.map.GameMap;
import org.crazydays.gameplan.map.GridType;

import org.crazydays.junit.TestHelper;

/**
 * DatabaseOutputStreamUnitTest
 */
public class DatabaseOutputStreamUnitTest
{
    /** test helper */
    protected TestHelper testHelper =
        new TestHelper(DatabaseOutputStreamUnitTest.class);

    /**
     * Cleanup test helper.
     */
    @After
    public void tearDown()
    {
        testHelper.tearDown();
    }

    /**
     * Test constructor.
     * 
     * @throws IOException
     */
    @Test(expected = ZipException.class)
    public void testConstructor()
        throws IOException
    {
        // objects
        ByteArrayOutputStream boas = new ByteArrayOutputStream();

        // test
        DatabaseOutputStream stream = new DatabaseOutputStream(boas);

        // verify: force a zip exception since no entry would
        // be created by default. No exception means it isn't
        // a ZipOutputStream internally.
        stream.write(new byte[] { 'a', 'b', 'c' });
    }

    /**
     * Test full export of multiple maps.
     * 
     * @throws IOException
     */
    @Test
    public void testIntegration()
        throws IOException
    {
        // objects
        Database database = new Database();
        database.setName("aname");
        database.addGameMap(new GameMap("grid", 480, 640, GridType.SQUARE, 20));
        database.addGameMap(new GameMap("hex", 480, 640, GridType.HEXAGON, 20));
        database.addGameMap(new GameMap("iso", 480, 640, GridType.DIAMOND, 20));

        File file = new File(testHelper.getSandbox(), "a.zip");
        FileOutputStream output = new FileOutputStream(file);

        // setup
        DatabaseOutputStream stream = new DatabaseOutputStream(output);

        // test
        stream.write(database);
        stream.close();

        // verify
        ZipFile zip = new ZipFile(file);
        assertEquals("count", 10, zip.size());
    }
}
